package sky.cin.framework.sfoc.core;

import sky.cin.framework.sfoc.core.annotations.beans.Lazy;
import sky.cin.framework.sfoc.core.annotations.beans.Scope;
import sky.cin.framework.sfoc.core.annotations.beans.ScopeType;
import sky.cin.framework.sfoc.core.common.utils.BeanUtils;
import sky.cin.framework.sfoc.core.interfaces.*;

public class SfocBeanDefinition implements BeanDefinition {
    
    private Class<?> clazz;
    private boolean lazy;
    private boolean singleton;
    
    public SfocBeanDefinition() {
    }
    
    public SfocBeanDefinition(Class<?> clazz, boolean lazy, boolean singleton) {
        this.clazz = clazz;
        this.lazy = lazy;
        this.singleton = singleton;
    }
    
    public Class<?> getClazz() {
        return clazz;
    }
    
    public void setClazz(Class<?> clazz) {
        this.clazz = clazz;
    }
    
    public boolean isLazy() {
        return lazy;
    }
    
    public boolean isSingleton() {
        return singleton;
    }
    
    public SfocBeanDefinition(Class<?> clazz) {
        setClazz(clazz);
        setLazy(clazz);
        setSingleton(clazz);
    }
    
    public void setLazy(boolean lazy) {
        this.lazy = lazy;
    }
    
    public void setLazy(Class<?> clazz) {
        Lazy annotation = BeanUtils.getAnnotation(clazz, Lazy.class);
        boolean lazy = annotation == null || annotation.value();
        setLazy(lazy);
    }
    
    public void setSingleton(boolean singleton) {
        this.singleton = singleton;
    }
    
    public void setSingleton(Class<?> clazz) {
        Scope annotation = BeanUtils.getAnnotation(clazz, Scope.class);
        ScopeType scope = annotation != null
            ? BeanUtils.getAnnotation(clazz, Scope.class).value()
            : ScopeType.SINGLETON;
        setSingleton(ScopeType.SINGLETON.equals(scope));
    }
    
}
